package com.lijing.binarySearch;

public class BinarySearch {
    public static void main(String[] args) {
        int[] arr = {1,8,10,50,59,89};
        System.out.println(binarySearch(arr,0,arr.length -1,11));
    }

    /**
     * binarySearch: the array must be sorted.
     * @param arr array
     * @param left left_index
     * @param right right_index
     * @param findVal find-->index,not find-->-1
     * @return index
     */
    public static int binarySearch(int[] arr, int left, int right, int findVal){
        if(left > right){
            return -1;
        }
        int mid = (left+right)/2;
        int midVal = arr[mid];
        if(findVal > midVal){
            return binarySearch(arr,mid+1,right,findVal);
        }
        if(findVal < midVal){
            return binarySearch(arr,left,mid-1,findVal);
        }else {
            return mid;
        }
    }
}
